package com.micro.ai.gateway.controller;

import com.micro.ai.commons.domain.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.actuate.GatewayControllerEndpoint;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@RestController
@RequestMapping("/actuator")
public class HealthController {

    @Autowired(required = false)
    private GatewayControllerEndpoint gatewayControllerEndpoint;

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Mono<ApiResponse<Map<String, Object>>> health() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", LocalDateTime.now());
        health.put("service", "micro-ai-gateway");
        health.put("version", "0.0.1");
        
        return Mono.just(ApiResponse.success("网关服务正常", health));
    }

    /**
     * 网关信息
     */
    @GetMapping("/gateway/info")
    public Mono<ApiResponse<Map<String, Object>>> gatewayInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("service", "micro-ai-gateway");
        info.put("description", "micro-ai API网关服务");
        info.put("version", "0.0.1");
        info.put("timestamp", LocalDateTime.now());
        
        if (gatewayControllerEndpoint != null) {
            info.put("routes", gatewayControllerEndpoint.routes());
        }
        
        return Mono.just(ApiResponse.success("网关信息", info));
    }
}
